﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PreProcessing
{
    class Counter
    {
        Dictionary<string, Int64> _1w = new Dictionary<string, Int64>();
        Dictionary<string, Int64> _2w = new Dictionary<string, Int64>();
        Dictionary<string, Int64> _3w = new Dictionary<string, Int64>();
        public void readfile(string path)
        {
            
            string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                StreamReader sr = new StreamReader(file);
                string input;
                while ((input = sr.ReadLine()) != null)
                {
                    input = Regex.Replace(input, @"\s{2,}", @" ");
                    process(input);
                }
                
            }
        }
        public void process(string inp)
        {
            string[] input = inp.Split(':');
            int amount = 0;
            if (input.Length == 2)
            {
                amount = int.Parse(input[1]);
            }
            else amount = 1;
            string[] data=input[0].Split(' ');
            for(int i=0;i<data.Length;i++)
            {
                if (_1w.ContainsKey(data[i]))
                {
                    _1w[data[i]] += amount;
                }
                else
                {
                    _1w.Add(data[i], amount);
                }
                if (i < data.Length - 1)
                {
                    string data2 = data[i] + " " + data[i + 1];
                    if (_2w.ContainsKey(data2))
                    {
                        _2w[data2] += amount;
                    }
                    else
                    {
                        _2w.Add(data2, amount);
                    }
                    //if (i < data.Length - 2)
                    //{
                    //    string data3 = data2 + " " + data[i + 2];
                    //    if (_3w.ContainsKey(data3))
                    //    {
                    //        _3w[data3] += amount;
                    //    }
                    //    else
                    //    {
                    //        _3w.Add(data3, amount);
                    //    }
                    //}
                }
            }
        }

        public void writefile(string path)
        {
            string pathO = path + @"\1word.txt";
            StreamWriter sw;
            sw = new StreamWriter(pathO);
            foreach (KeyValuePair<string, Int64> pair in _1w)
            {
                sw.WriteLine(pair.Key + ":" + pair.Value);
            }
            sw.Flush();
            sw.Close();
            pathO = path + @"\2word.txt";
            sw = new StreamWriter(pathO);
            foreach (KeyValuePair<string, Int64> pair in _2w)
            {
                sw.WriteLine(pair.Key + ":" + pair.Value);
            }
            sw.Flush();
            sw.Close();
            //pathO = path + @"\3word.txt";
            //sw = new StreamWriter(pathO);
            //foreach (KeyValuePair<string, Int64> pair in _3w)
            //{
            //    sw.WriteLine(pair.Key + ":" + pair.Value);
            //}
            //sw.Flush();
            //sw.Close();
        }

        public void count(string path)
        {
            List<string> s10 = new List<string>();
            List<string> s5 = new List<string>();
            List<string> s3 = new List<string>();
            List<string> s2 = new List<string>();
            List<string> s1 = new List<string>();
            string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                StreamReader sr = new StreamReader(file);
                string inp;
                while ((inp = sr.ReadLine()) != null)
                {
                    string[] input = inp.Split(':');
                    int amount;
                    if (input.Length == 2)
                    {
                        amount = int.Parse(input[1]);
                    }
                    else amount = 1;
                    
                    if (amount >= 10) s10.Add(inp);
                    else if (amount >= 5) s5.Add(inp);
                    else if (amount >= 3) s3.Add(inp);
                    else if (amount >= 2) s2.Add(inp);
                    else s1.Add(inp);
                }

            }
            string pathO = path + @"\s10.txt";
            StreamWriter sw = new StreamWriter(pathO);
            foreach (string s in s10)
            {
                sw.WriteLine(s);
            }
            sw.Flush();
            sw.Close();
            pathO = path + @"\s5.txt";
            sw = new StreamWriter(pathO);
            foreach (string s in s5)
            {
                sw.WriteLine(s);
            }
            sw.Flush();
            sw.Close();
            pathO = path + @"\s3.txt";
            sw = new StreamWriter(pathO);
            foreach (string s in s3)
            {
                sw.WriteLine(s);
            }
            sw.Flush();
            sw.Close();
            pathO = path + @"\s2.txt";
            sw = new StreamWriter(pathO);
            foreach (string s in s2)
            {
                sw.WriteLine(s);
            }
            sw.Flush();
            sw.Close();
            pathO = path + @"\s1.txt";
            sw = new StreamWriter(pathO);
            foreach (string s in s1)
            {
                sw.WriteLine(s);
            }
            sw.Flush();
            sw.Close();
        }
        public void count5a1(string path)
        {
            List<string> sens5 = new List<string>();
            List<string> sens12 = new List<string>();
            List<string> sens34 = new List<string>();
            string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                StreamReader sr = new StreamReader(file);
                string inp;
                while ((inp = sr.ReadLine()) != null)
                {
                    string[] input = inp.Split(':');
                    int amount;
                    if (input.Length == 2)
                    {
                        amount = int.Parse(input[1]);
                    }
                    else amount = 1;
                    string sens=input[0];
                    if (sens.Split(' ').Length >= 4)
                    {
                        if (amount >= 5) sens5.Add(sens);
                        else
                        {
                            string[] str = sens.Split(' ');
                            bool isViet = true;
                            foreach (string s in str)
                            {
                                if (!Regex.IsMatch(Pattern.ConvertToUnSign(s), @"\b(^(r|d|đ|gi|v|ch|tr|s|x|l|n|q|b|c|k|g|gh|h|kh|m|ng|ngh|nh|p|ph|t|th))?(u|e|o|a|i|y|ie|uo|ai|au|ua|ia|ui|eu|oi|ay|uy|ou|ua|oa|ao|eo|ue|uya|uoi|uye|ieu|oai|oay|uoi|uou)((c|ch|m|n|ng|nh|u|p|t)$)?\b")) isViet=false;
                            }
                            if (isViet)
                            {
                                if (amount >= 3)
                                    sens34.Add(sens);
                                else sens12.Add(sens);
                            }
                        }
                    }
                }
                sr.Close();
            }
            string pathO = path + @"\sens5.txt";
            StreamWriter sw = new StreamWriter(pathO);
            foreach (string s in sens5)
            {
                sw.WriteLine(s);
            }
            sw.Flush();
            sw.Close();
            pathO = path + @"\sens34.txt";
            sw = new StreamWriter(pathO);
            foreach (string s in sens34)
            {
                sw.WriteLine(s);
            }
            sw.Flush();
            sw.Close();
            pathO = path + @"\sens12.txt";
            sw = new StreamWriter(pathO);
            foreach (string s in sens12)
            {
                sw.WriteLine(s);
            }
            sw.Flush();
            sw.Close();
        }

        public void count12(string path,string pathO)
        {
            List<string> sens12 = new List<string>();
            StreamReader sr = new StreamReader(path);
            string s;
            int count = 0;
            Random rd = new Random();
            while ((s = sr.ReadLine()) != null && count<=100000)
            {
                if (rd.Next(100) >70||s.Split(' ').Length>5)
                {
                    sens12.Add(s);
                    count++;
                }
            }
            sr.Close();
            StreamWriter sw = new StreamWriter(pathO);
            foreach (string sen in sens12)
            {
                sw.WriteLine(sen);
            }
            sw.Flush();
            sw.Close();
        }

        public Int64 countsens(string path)
        {
            string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
            Int64 cc = 0;
            foreach (string file in files)
            {
                StreamReader sr = new StreamReader(file);
                string inp;
                while ((inp = sr.ReadLine()) != null)
                {
                    cc++;
                }
                sr.Close();
            }
            return cc;
        }
        public bool checkTV(string inp)
        {
            foreach (string s in inp.Split(' '))
            {
                if (!Regex.IsMatch(Pattern.ConvertToUnSign(s), @"\b(^(r|d|đ|gi|v|ch|tr|s|x|l|n|q|b|c|k|g|gh|h|kh|m|ng|ngh|nh|p|ph|t|th))?(u|e|o|a|i|y|ie|uo|ai|au|ua|ia|ui|eu|oi|ay|uy|ou|ua|oa|ao|eo|ue|uya|uoi|uye|ieu|oai|oay|uoi|uou)((c|ch|m|n|ng|nh|u|p|t)$)?\b"))
                {
                    return false;
                }
            }
            return true;
        }
        public void count3word(string path)
        {
            string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                StreamReader sr = new StreamReader(file);
                string inp;
                while ((inp = sr.ReadLine()) != null)
                {
                    string[] input = inp.Split(':');
                    int amount = 0;
                    if (input.Length == 2)
                    {
                        amount = int.Parse(input[1]);
                    }
                    else amount = 1;
                    string[] data = input[0].Split(' ');
                    for (int i = 0; i < data.Length; i++)
                    {

                        if (i < data.Length - 2)
                        {
                            string data3 = data[i] + " " + data[i + 1] + " " + data[i + 2];
                            if (checkTV(data3))
                            {
                                if (_3w.ContainsKey(data3))
                                {
                                    _3w[data3] += amount;
                                }
                                else
                                {
                                    _3w.Add(data3, amount);
                                }
                            }
                        }

                    }
                }
            }
            string pathO = path + @"\3word.txt";
            StreamWriter sw;
            sw = new StreamWriter(pathO);
            foreach (KeyValuePair<string, Int64> pair in _3w)
            {
                sw.WriteLine(pair.Key + ":" + pair.Value);
            }
            sw.Flush();
            sw.Close();
        }
    }
}
